﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class BezierComp : MonoBehaviour {
    public enum BezierRunType
    {
        Time,
        Distance,
    }
    public Vector3 zhou;
    public float up;
    public float rSpeed;
    public Vector3 point;

    public Transform m_tf;
    private Vector3 m_v3StartPoint;
    private Vector3 m_v3Dir;
    private Quaternion m_Quaternion;
    private float m_fHeight;
    private bool m_bCheckHeight = false;
    private List<BezierPath.PathPoint> m_Point;
    private float m_fLineDis;
    private float m_fDuration;
    private float m_fPassTime;
    private float m_fStartTime;
    private bool m_bWork = false;
    private Action m_FlyOver;
    private BezierRunType m_RunType;
    private Vector3 deltaPos;

    private float PassTime
    {
        get
        {
            return (SlgBattleMgr.Instance == null ? (Time.time) : (float)SlgBattleMgr.Instance.PassTime);
        }
    }

    public void InitBezier(Transform _tf,string key,float time,Vector3 dir,BezierRunType type,Action arrive)
    {
        m_v3StartPoint = _tf.transform.position;
        m_Point = Assets.Script.Config.ConfigManager.Instance.bezierLoader.GetBezier(key);
        m_tf = _tf;
        m_v3Dir = dir;
        m_Quaternion = Quaternion.FromToRotation(Vector3.left, m_v3Dir);
        m_fDuration = time;
        m_fPassTime = 0;
        m_fStartTime = PassTime;
        m_RunType = type;
        m_FlyOver = arrive;
        m_bCheckHeight = false;
        if (m_Point != null)
        {
            m_bWork = true;
            BezierPath.InitPoints(m_Point);
            m_fLineDis = 0;
            for (int i = 0; i < m_Point.Count; i++)
            {
                m_fLineDis += m_Point[i].distance;
            }
        }
    }
    public void InitBezier(Transform _tf, List<BezierPath.PathPoint> points, float time, BezierRunType type, Action arrive)
    {
        m_v3StartPoint = _tf.transform.position;
        m_Point = points;
        m_tf = _tf;
        m_v3Dir = m_tf.forward;
        m_Quaternion = Quaternion.FromToRotation(Vector3.right, m_v3Dir);
        m_fDuration = time;
        m_fPassTime = 0;
        m_fStartTime = PassTime;
        m_RunType = type;
        m_FlyOver = arrive;
        m_bCheckHeight = false;
        if (m_Point != null)
        {
            m_bWork = true;
            BezierPath.InitPoints(m_Point);
            m_fLineDis = 0;
            for (int i = 0; i < m_Point.Count; i++)
            {
                m_fLineDis += m_Point[i].distance;
            }
        }
    }
    public void InitBezier(Transform _tf, string key, float time, Vector3 dir, BezierRunType type,float height ,Action arrive)
    {
        InitBezier(_tf, key, time, dir, type, arrive);
        m_fHeight = height;
        m_bCheckHeight = true;
    }
    public static Vector3 GetBezierValueByTime(float duration,float value,List<BezierPath.PathPoint> beziers)
    {
        Vector3 v = Vector3.zero;
        float pct = value / duration;
        int num = beziers.Count;
        v = BezierPath.GetPositionByT(pct * (float)num, beziers);
        return v;
    }
    public static Vector3 GetBezierValueByDistance(float duration, float value,float totalDis,List<BezierPath.PathPoint> beziers)
    {
        Vector3 v = Vector3.zero;
        float pct = Mathf.Clamp01(value / duration);
        float dis = totalDis * pct;
        v = BezierPath.GetPositionByDistance(dis, beziers);
        return v;
    }
    private void Update()
    {
        if (!m_bWork)
            return;

        m_fPassTime = (PassTime - m_fStartTime);
        if(m_RunType == BezierRunType.Time)
        {
            deltaPos = m_Quaternion * GetBezierValueByTime(m_fDuration, m_fPassTime, m_Point);
            m_tf.transform.position = m_v3StartPoint + deltaPos;


        }
        else if(m_RunType == BezierRunType.Distance)
        {
            deltaPos = m_Quaternion * GetBezierValueByDistance(m_fDuration, m_fPassTime, m_fLineDis, m_Point);
            m_tf.transform.position = m_v3StartPoint + deltaPos;
        }
        if (m_bCheckHeight)
        {
            if(deltaPos.y >= m_fHeight)
            {
                m_bWork = false;
                if (m_FlyOver != null)
                    m_FlyOver();

                return;
            }
        }

        //旋转
        m_tf.Rotate(SlgBattleProperty.Instance.zhou, SlgBattleProperty.Instance.rSpeed);

        if (m_fPassTime >= m_fDuration)
        {
            m_bWork = false;
            if (m_FlyOver != null)
                m_FlyOver();
        }

        
    }
}
